'use strict';
// 菜单管理
app.controller('menu', ['$scope', '$http', 'commonFactory', 'toaster', '$modal', '$log', function ($scope, $http, commonFactory, toaster, $modal, $log) {
    $scope.menuData = {
        Index: '1',
        Size: '100'
    };
    var initdata = function () {
        // //下一页
        // $scope.next = function () {
        //     if ($scope.page) {
        //         if ($scope.menuData.pageIndex >= $scope.page) {//判断最大页码
        //             $scope.menuData.pageIndex = $scope.page;
        //             return false;
        //         }
        //         $scope.menuData.pageIndex++
        //         requestMenuList();
        //     }
        // }
        // //上一页
        // $scope.prev = function () {
        //     if ($scope.menuData.pageIndex <= 1) {//判断最小页码
        //         $scope.menuData.pageIndex = 1;
        //         return false;
        //     }
        //     $scope.menuData.pageIndex--;
        //     requestMenuList()
        // }
        //查询
        $scope.search = function () {
            $scope.menuData.pageIndex = 1 //查询后在第一页显示
            requestMenuList();
        }
        // 添加
        $scope.addMenu = function () {
            localStorage.setItem("menu", '');
            var modalInstance = $modal.open({
                templateUrl: 'tpl/model/delPmodel.html?v='+Math.random(),
                controller: ['$scope', '$modalInstance', 'toaster', 'commonFactory', '$timeout', function ($scope, $modalInstance, toaster, commonFactory, $timeout) {
                    $scope.addMenushow = true;
                    $scope.Menuitems = {
                        Name: '',
                        Url: ''
                    }
                    $scope.istrue = false;
                    $scope.isadd = true;
                    $scope.addMenus = function () {
                        if (checkinp() && $scope.isadd) {
                            $scope.isadd = false
                            localStorage.setItem("menu", '');
                            commonFactory.loading(true)
                            commonFactory.ajax("/api/Menu/AddOrUpdateMenu", $scope.Menuitems, function (result) {
                                commonFactory.loading(false)
                                if (result.Code == 1) {
                                    $scope.isadd = true;
                                    toaster.pop('success', '', '添加菜单成功');
                                    $modalInstance.close('cancel');
                                } else {
                                    toaster.pop('warning', '', '添加菜单失败');
                                    $modalInstance.close('cancel');
                                }
                            }, function () {
                                toaster.pop('error', '', '后台错误');
                            }, "post");
                        }
                    }
                    var checkinp = function () {
                        if ($scope.Menuitems.Name === "") {
                            toaster.pop('warning', '', '请填写菜单名');
                            return
                        }
                        if (!$scope.Menuitems.Url) {
                            toaster.pop('warning', '', '请填写菜单URL ');
                            return
                        }
                        return true
                    }
                    $scope.cancel = function () {
                        $modalInstance.close('cancel');
                    };
                }],
                size: '',
                backdrop: false
            });
            modalInstance.result.then(function (selectedItem) {
                requestMenuList()
            }, function () {

            });
        }

        var requestMenuList = function () {
            commonFactory.loading(true)
            commonFactory.ajax("/api/Menu/QueryMenus", $scope.menuData, function (result) {
                commonFactory.loading(false)
                if (result && result != '') {
                    $scope.QueryMenuList = result.Data.Data;
                    $scope.QueryMenuCount = result.Data.Count;
                    $scope.page = Math.ceil(result.Count / $scope.size);
                } else {
                    toaster.pop('warning', '成功', "后台没有数据");
                }
            }, function () {
                commonFactory.loading(false)
                toaster.pop('error', '失败', '后台未返回报错');
            }, "post");
        }
        requestMenuList();
        $scope.delcache = function () {
            commonFactory.ajax("/api/ConfigCenters/DeleteCache", {}, function (result) {
                if (result.Code == 1) {
                    toaster.pop('success', '', '已清除缓存')
                }
            }, function () { }, 'post')
        }

        // 编辑
        $scope.editMenu = function (obj) {
            localStorage.setItem("menu", JSON.stringify(obj));
            var modalInstance = $modal.open({
                templateUrl: 'tpl/model/delPmodel.html?v='+Math.random(),
                controller: ['$scope', '$modalInstance', 'toaster', 'commonFactory', '$timeout', function ($scope, $modalInstance, toaster, commonFactory, $timeout) {
                    $scope.editMenushow = true;
                    $scope.Menuitems = {
                        Id: '',
                        Name: '',
                        Url: ''
                    }
                    $scope.isadd = true;
                    var menulist = localStorage.getItem("menu")
                    if (menulist) {
                        $scope.istrue = true;
                        menulist = JSON.parse(menulist);
                        $scope.Menuitems.Id = menulist.Id;
                        $scope.Menuitems.Name = menulist.Name;
                        $scope.Menuitems.Url = menulist.Url;
                    }
                    $scope.editMenus = function () {
                        if (checkinp() && $scope.isadd) {
                            $scope.isadd = false
                            commonFactory.loading(true)
                            commonFactory.ajax("/api/Menu/AddOrUpdateMenu", $scope.Menuitems, function (result) {
                                commonFactory.loading(false)
                                if (result.Code == 1) {
                                    $scope.isadd = true;
                                    toaster.pop('success', '', '编辑菜单成功');
                                    $modalInstance.close('cancel');
                                } else {
                                    toaster.pop('warning', '', '编辑菜单失败');
                                    $modalInstance.close('cancel');
                                }
                            }, function () {
                                commonFactory.loading(false)
                                toaster.pop('error', '', '后台错误');
                            }, "post");
                        }
                    }
                    var checkinp = function () {
                        if ($scope.Menuitems.Name === "") {
                            toaster.pop('warning', '', '请填写菜单名');
                            return
                        }
                        if (!$scope.Menuitems.Url) {
                            toaster.pop('warning', '', '请填写菜单URL ');
                            return
                        }
                        return true
                    }
                    $scope.cancel = function () {
                        $modalInstance.close('cancel');
                    };
                }],
                size: '',
                backdrop: false
            });
            modalInstance.result.then(function (selectedItem) {
                requestMenuList()
            }, function () {

            });
        }
        // 删除
        $scope.delMenu = function (obj) {
            var modalInstance = $modal.open({
                templateUrl: 'tpl/model/delPmodel.html?v='+Math.random(),
                controller: ['$scope', '$modalInstance', 'items', 'toaster', 'commonFactory', function ($scope, $modalInstance, items, toaster, commonFactory) {
                    $scope.items = items;
                    $scope.ok = function () {
                        //删除
                        commonFactory.loading(true)
                        commonFactory.ajax("/api/Menu/Remove/" + $scope.items, {}, function (result) {
                            commonFactory.loading(false);
                            if (result.Code == 1) {
                                toaster.pop('success', '', '删除菜单成功');
                                $modalInstance.close();
                            } else {
                                toaster.pop('error', '', '删除菜单失败');
                                $modalInstance.close();
                            }

                        }, function () {
                            commonFactory.loading(false)
                        }, "post");
                    }

                    $scope.cancel = function () {
                        $modalInstance.close('cancel');
                    };
                }],
                size: 'sm',
                backdrop: false,
                resolve: {
                    items: function () {
                        return obj;
                    }
                }
            });
            modalInstance.result.then(function (selectedItem) {
                requestMenuList();
            }, function () {

            });
        }
    }
    initdata();

}]);

// 配置中心
app.controller('setting', ['$scope', '$http', 'commonFactory', 'toaster', '$modal', '$log', function ($scope, $http, commonFactory, toaster, $modal, $log) {
    $scope.items = {
        ConfigDesc: '',
        StartTime: '',
        EndTime: '',
        ConfigKey: '',
        ConfigValue: ''
    };
    var initdata = function () {
        $scope.index = 1;
        $scope.size = 5;
        //下一页
        $scope.next = function () {
            if ($scope.page) {
                if ($scope.index >= $scope.page) {//判断最大页码
                    $scope.index = $scope.page;
                    return false;
                }
                $scope.index++
                requestProductList();
            }
        }
        //上一页
        $scope.prev = function () {
            if ($scope.index <= 1) {//判断最小页码
                $scope.index = 1;
                return false;
            }
            $scope.index--;
            requestProductList()
        }
        $scope.requestArr = {//查询条件
            ProductName: '',
            ProductCode: '',
            ProductGroup: '',
            UserId: '',
            ProductStatus: ''
        }
        //查询
        $scope.search = function () {
            $scope.index = 1 //查询后在第一页显示
            $scope.ProductName = $scope.requestArr.ProductName;//model重新赋值 传入服务器
            $scope.ProductCode = $scope.requestArr.ProductCode;
            $scope.ProductGroup = $scope.requestArr.ProductGroup;
            $scope.UserId = $scope.requestArr.UserId;
            $scope.ProductStatus = $scope.requestArr.ProductStatus;
            requestProductList();
        }
        // 添加
        $scope.addSet = function () {
            var modalInstance = $modal.open({
                templateUrl: 'tpl/model/delPmodel.html?v='+Math.random(),
                controller: ['$scope', '$modalInstance', 'toaster', 'commonFactory', '$timeout', function ($scope, $modalInstance, toaster, commonFactory, $timeout) {
                    $scope.addSetitems = {
                        ConfigDesc: '',
                        StartTime: '',
                        EndTime: '',
                        ConfigKey: '',
                        ConfigValue: ''
                    };
                    // $scope.StartTime = new Date();
                    // $scope.endTime = new Date();
                    // $scope.addSetitems.StartTime = $scope.StartTime;
                    // $scope.addSetitems.EndTime = $scope.endTime;
                    var checkinp = function () {
                        if (!$scope.addSetitems.ConfigDesc) {
                            toaster.pop('warning', '', '请填写描述信息');
                            return
                        }
                        if (!$scope.addSetitems.StartTime) {
                            toaster.pop('warning', '', '请填写起始时间');
                            return
                        }
                        if (!$scope.addSetitems.EndTime) {
                            toaster.pop('warning', '', '请填写结束时间');
                            return
                        }
                        if (!$scope.addSetitems.ConfigKey) {
                            toaster.pop('warning', '', '请填写key值');
                            return
                        }
                        if (!$scope.addSetitems.ConfigValue) {
                            toaster.pop('warning', '', '请填写内容');
                            return
                        }
                        return true
                    }
                    $scope.noclick = true
                    $scope.ok = function () {
                        $scope.addSetitems.StartTime = commonFactory.DateFormat($scope.addSetitems.StartTime)
                        $scope.addSetitems.EndTime = commonFactory.DateFormat($scope.addSetitems.EndTime)
                        $scope.noclick = false
                        if (checkinp()) {
                            commonFactory.loading(true)
                            commonFactory.ajax("/api/ConfigCenters/AddConfig", $scope.addSetitems, function (result) {
                                commonFactory.loading(false)
                                $scope.noclick = true
                                if (result && result != '') {
                                    toaster.pop('success', '', '成功');
                                    $modalInstance.close('cancel');
                                } else {
                                    toaster.pop('error', '失败', 'del');
                                }
                            }, function () {
                                commonFactory.loading(false)
                                toaster.pop('error', '失败', '后台未返回报错');
                            }, "post");
                        }
                    };

                    $scope.cancel = function () {
                        $modalInstance.close('cancel');
                    };
                }],
                size: '',
                backdrop: false
            });
            modalInstance.result.then(function (selectedItem) {
                requestProductList()
            }, function () {

            });
        }

        var requestProductList = function () {
            commonFactory.loading(true)
            var data={//传入参数
                Index: $scope.index,
                Size: $scope.size, 
                ProductName: $scope.ProductName || '', 
                ProductStatus: $scope.ProductStatus || '', 
                ProductCode: $scope.ProductCode || '', 
                ProductGroup: $scope.ProductGroup || '', 
                UserId: $scope.UserId || '' 
            }
            commonFactory.ajax("/api/ConfigCenters/QueryConfigList", data, function (result) {
                commonFactory.loading(false)
                if (result && result != '') {
                    $scope.QueryLoanProductList = result.Data;
                    $scope.QueryLoanProductCount = result.Count;
                    $scope.page = Math.ceil(result.Count / $scope.size);

                } else {
                    toaster.pop('error', '失败', "heihei");
                }
            }, function () {
                commonFactory.loading(false)
                toaster.pop('error', '失败', '后台未返回报错');
            }, "post");
        }
        requestProductList();
        //禁用启用
        // $scope.Release = function (pid) {
        //     commonFactory.ajax("/api/Product/ReleaseProduct", { '': pid }, function (result) {
        //         if (result.Code == 1) {
        //             toaster.pop('success', '成功', '发布成功');
        //             requestProductList()
        //         } else {
        //             toaster.pop('error', '失败', result.ResposeData);
        //         }
        //     }, function () {
        //         toaster.pop('error', '失败', '后台未返回报错');
        //     }, "post");
        // }
        $scope.delcache = function () {
            commonFactory.ajax("/api/ConfigCenters/DeleteCache", {}, function (result) {
                if (result.Code == 1) {
                    toaster.pop('success', '', '已清除缓存')
                }
            }, function () { }, 'post')
        }
        $scope.editset = function (sid) {
            // 编辑图片
            localStorage.setItem("Set", sid);
            var modalInstance = $modal.open({
                templateUrl: 'tpl/model/delPmodel.html?v='+Math.random(),
                controller: ['$scope', '$modalInstance', 'toaster', 'commonFactory', '$timeout', function ($scope, $modalInstance, toaster, commonFactory, $timeout) {
                    $scope.editSetitems = {};
                    $scope.StartTime = new Date();
                    $scope.endTime = new Date();
                    $scope.editSetitems.StartTime = $scope.StartTime;
                    $scope.editSetitems.EndTime = $scope.endTime;
                    var sid = localStorage.getItem("Set");
                    commonFactory.ajax("/api/ConfigCenters/GetConfigCenterById", { "": sid }, function (result) {
                        $scope.editSetitems = result
                    }, function () {
                        toaster.pop('warning', '', '后台错误')
                    }, 'post')
                    var checkinp = function () {
                        if (!$scope.editSetitems.ConfigDesc) {
                            toaster.pop('warning', '', '请填写描述信息');
                            return
                        }
                        if (!$scope.editSetitems.StartTime) {
                            toaster.pop('warning', '', '请填写起始时间');
                            return
                        }
                        if (!$scope.editSetitems.EndTime) {
                            toaster.pop('warning', '', '请填写结束时间');
                            return
                        }
                        if (!$scope.editSetitems.ConfigKey) {
                            toaster.pop('warning', '', '请填写key值');
                            return
                        }
                        if (!$scope.editSetitems.ConfigValue) {
                            toaster.pop('warning', '', '请填写内容');
                            return
                        }
                        return true
                    }
                    $scope.ok = function () {
                        $scope.editSetitems.StartTime = commonFactory.DateFormat($scope.editSetitems.StartTime)
                        $scope.editSetitems.EndTime = commonFactory.DateFormat($scope.editSetitems.EndTime)
                        delete $scope.editSetitems.CreateTime;
                        if (checkinp()) {
                            commonFactory.loading(true)
                            commonFactory.ajax("/api/ConfigCenters/AddConfig", $scope.editSetitems, function (result) {
                                commonFactory.loading(false)
                                if (result && result != '') {
                                    toaster.pop('success', '', '成功');
                                    $timeout(function () {
                                        commonFactory.go('/app/setting/setting')
                                    }, 1000)
                                } else {
                                    toaster.pop('error', '失败', 'del');
                                }
                            }, function () {
                                commonFactory.loading(false)
                                toaster.pop('error', '失败', '后台未返回报错');
                            }, "post");
                        }
                    };

                    $scope.cancel = function () {
                        $modalInstance.close('cancel');
                    };
                }],
                size: '',
                backdrop: false
            });
            modalInstance.result.then(function (selectedItem) {
                requestProductList()
            }, function () {

            });
        }
    }
    initdata();

}]);
//员工
app.controller('staff', ['$scope', '$http', 'commonFactory', 'toaster', '$modal', '$log', function ($scope, $http, commonFactory, toaster, $modal, $log) {
    $scope.addstaff = function () {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/model/delPmodel.html?v='+Math.random(),
            controller: ['$scope', '$modalInstance', function ($scope, $modalInstance) {
                $scope.addmsg = {
                    FullName: '',
                    UserName: '',
                    Password: ''
                }
                var checkmsg = function () {
                    if (!$scope.addmsg.FullName) {
                        toaster.pop('warning', '', '请填写用户名')
                        return;
                    }
                    if (!$scope.addmsg.UserName) {
                        toaster.pop('warning', '', '请填写用户名')
                        return;
                    }
                    if (!$scope.addmsg.Password) {
                        toaster.pop('warning', '', '请填写密码')
                        return;
                    }
                    return true;
                }
                $scope.isadd = true;
                $scope.addpeople = function () {
                    if (checkmsg()) {
                        $scope.isadd = false;
                        commonFactory.loading(true)
                        commonFactory.ajax("/api/Staff/AddStaff", $scope.addmsg, function (result) {
                            commonFactory.loading(false)
                            $scope.isadd = true;
                            if (result.Code == 1) {
                                toaster.pop('success', '成功', '发布成功');
                                $modalInstance.close();
                            } else {
                                toaster.pop('error', '失败', result.ResposeData);
                                $modalInstance.close();
                            }
                        }, function () {
                            toaster.pop('error', '失败', '后台未返回报错');
                        }, "post");
                    }

                }
                $scope.cancel = function () {
                    $modalInstance.close();
                };
            }
            ],
            size: '',
            backdrop: false
        });
        modalInstance.result.then(function () {
            initdata()
        }, function () {
        });
    }
    $scope.setMenu = function (uId) {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/model/delPmodel.html?v='+Math.random(),
            controller: ['$scope', '$modalInstance', function ($scope, $modalInstance) {
                $scope.setMenushow = true;
                $scope.setMenuData = {
                    UserId: uId,
                    Menus: []
                }
                var requestMenuList = function () {
                    commonFactory.loading(true)
                    commonFactory.ajax("/api/Menu/QueryMenus", { Index: '1', Size: '100' }, function (result) {
                        commonFactory.loading(false)
                        if (result && result != '') {
                            $scope.AllMenuList = result.Data.Data;
                            commonFactory.ajax("/api/Staff/QueryStaffMenus?userId=" + uId, {}, function (result) {
                                if (result && result != '') {
                                    $scope.setMenuList = result.Data;
                                    if ($scope.setMenuList.Menus.length != 0) {
                                        var checkbox = $("input[name=menucheckout]");
                                        for (var i = $scope.setMenuList.Menus.length - 1; i >= 0; i--) {
                                            for (var j = checkbox.length - 1; j >= 0; j--) {
                                                if (checkbox[j].value == $scope.setMenuList.Menus[i]) {
                                                    checkbox[j].checked = "checked";
                                                }
                                            }
                                        }
                                    }
                                } else {
                                    toaster.pop('warning', '成功', "用户没有订制数据");
                                }
                            }, function () {
                                toaster.pop('error', '失败', '后台未返回报错');
                            }, "post");
                        } else {
                            toaster.pop('warning', '成功', "后台没有菜单数据");
                        }
                    }, function () {
                        commonFactory.loading(false)
                        toaster.pop('error', '失败', '后台未返回报错');
                    }, "post");
                }
                requestMenuList()
                var checkchose = function () {

                    return true;
                }
                $scope.ok = function () {
                    var checkbox = $("input[name=menucheckout]:checked")
                    for (var i = checkbox.length - 1; i >= 0; i--) {
                        $scope.setMenuData.Menus.push(Number(checkbox[i].value));
                    }
                    var uId = localStorage.getItem('u_id');
                    $scope.setMenuData.u_id = uId;
                    $http({
                        url: commonFactory.baseurl + "/api/Staff/AddStaffMenus",
                        method: "POST",
                        data: $scope.setMenuData,
                        timeout: 60 * 1000
                    }).success(function (result) {
                        if (result.Code == 1) {
                            toaster.pop('success', '成功', '菜单订置成功');
                            $modalInstance.close();
                        } else {
                            toaster.pop('error', '失败', result.ResposeData);
                            $modalInstance.close();
                        }
                    }).error(function () {
                        if (typeof err == "function") {
                            toaster.pop('error', '失败', '请求失败，请稍后再试');
                            $modalInstance.close();
                        }
                    });
                    // commonFactory.ajax("/api/Staff/AddStaffMenus", $scope.setMenuData, function (result) {
                    //     if (result.Code == 1) {
                    //         toaster.pop('success', '成功', '菜单订置成功');
                    //         $modalInstance.close();
                    //     } else {
                    //         toaster.pop('error', '失败', result.ResposeData);
                    //         $modalInstance.close();
                    //     }
                    // }, function () {
                    //     toaster.pop('error', '失败', '后台未返回报错');
                    // }, "post");
                }

                $scope.cancel = function () {
                    $modalInstance.close();
                };
            }
            ],
            size: '',
            backdrop: false,
            resolve: {
                addstaff: function () {
                    return;
                }
            }
        });
    }
    $scope.dele = function (uId) {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/model/delPmodel.html?v='+Math.random(),
            controller: ['$scope', '$modalInstance', 'deleId', function ($scope, $modalInstance, deleId) {
                $scope.deleId = deleId;
                $scope.ok = function () {
                    commonFactory.loading(true)
                    commonFactory.ajax("/api/Staff/Remove?userId=" + deleId, {}, function (result) {
                        commonFactory.loading(false)
                        if (result.Code == 1) {
                            toaster.pop('success', '', "删除成功");
                            $modalInstance.close()
                        }
                    }, function () {
                        commonFactory.loading(false)
                        toaster.pop('error', '失败', '后台未返回报错');
                    }, "post");
                }
                $scope.cancel = function () {
                    $modalInstance.close();
                };
            }
            ],
            size: 'sm',
            backdrop: false,
            resolve: {
                deleId: function () {
                    return uId;
                }
            }
        });
        modalInstance.result.then(function () {
            initdata()
        }, function () {
        });
    }
    var initdata = function () {
        commonFactory.loading(true)
        commonFactory.ajax("/api/Staff/Query", { index: '1', size: '25' }, function (result) {
            commonFactory.loading(false)
            if (result.Code == 1) {
                $scope.Query = result.Data.Data
            }
        }, function () {
            commonFactory.loading(false)
            toaster.pop('error', '失败', '后台未返回报错');
        }, "post");
    }
    initdata()
}]);
//login
app.controller('login', ['$scope', '$http', 'commonFactory', 'toaster', '$modal', '$log', function ($scope, $http, commonFactory, toaster, $modal, $log) {
    $scope.login = {
        username: "",
        password: ""
    }
    $scope.authError = '请输入用户名'
    var checklogin = function () {
        if (!$scope.login.username) {
            $scope.authError = '请输入用户名'
            return;
        }
        if (!$scope.login.password) {
            $scope.authError = '请输入密码'
            return;
        }
        return true
    }
    $scope.gologin = function () {
        if (checklogin()) {
            $scope.authError = ''
            commonFactory.loading(true)
            commonFactory.ajax("/api/Staff/Login", $scope.login, function (result) {
                commonFactory.loading(false)
                if (result.Code == 1) {
                    localStorage.setItem('u_id', result.Data)
                    commonFactory.go('/index')
                } else {
                    $scope.authError = '登陆失败请重新登陆！'
                }
            }, function () {
                commonFactory.loading(false)
            }, 'post')
        }

    }

}]);